/**
 * @author zhaozeyang
 * @date 2018/10/31 19:46
 * @description
 */
public class 三十八 {

    public static void main(String[] args) {
        System.out.println(countAndSay(1));
        System.out.println(countAndSay(2));
        System.out.println(countAndSay(3));
        System.out.println(countAndSay(4));
        System.out.println(countAndSay(5));
        System.out.println(countAndSay(6));
        System.out.println(countAndSay(7));
        System.out.println(countAndSay(8));
    }

    public static String countAndSay(int n) {
        Integer[] e = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        if (n < 1 || n > 30) {
            throw new RuntimeException();
        }
        if (n == 1) {
            return "1";
        }
        String result = "1";
        for (int i = 2; i <= n; i++) {
            result = count(result, e);
        }
        return result;
    }

    public static String count(String n, Integer[] e) {
        String result = "";
        char[] a = n.toCharArray();
        int count = 0;
        if (n.equals("1")) {
            return "11";
        }
        for (int i = 0; i < a.length; i++) {

            boolean isOut = i == a.length - 1 || !String.valueOf(a[i]).equals(String.valueOf(a[i + 1]));
            if (count == 0) {
                count++;
            } else if (String.valueOf(a[i]).equals(String.valueOf(a[i - 1]))) {
                count++;
            }
            if (isOut) {
                int index = Integer.valueOf(String.valueOf(a[i]));
                int num = e[index];
                result = result.concat(String.valueOf(count)).concat(String.valueOf(num));
                count = 0;
                continue;
            }
        }
        return result;
    }
}
